package com.sk.sourcecircle.activity.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.base.BaseActivity;
import com.sk.sourcecircle.callback.HandleDataCallBack;
import com.sk.sourcecircle.utils.AppApi;
import com.sk.sourcecircle.utils.MyUtils;
import com.sk.sourcecircle.utils.SnackUtils;
import com.sk.sourcecircle.view.VerificationCodeButton;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by sk on 2018/1/22.
 */

public class ForgetPasswordActivity extends BaseActivity implements HandleDataCallBack {
    @BindView(R.id.cancel)
    TextView cancel;
    @BindView(R.id.ed_account)
    EditText edAccount;
    @BindView(R.id.new_password)
    EditText newPassword;
    @BindView(R.id.confirm_password)
    EditText confirmPassword;
    @BindView(R.id.ed_code)
    EditText edCode;
    @BindView(R.id.get_code)
    VerificationCodeButton getCode;
    @BindView(R.id.login)
    TextView login;
    private LoadingDialog loadingDialog;

    @Override
    protected int getContentView() {
        return R.layout.activity_forget_password;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadingDialog = MyUtils.getLoadingDialog(mContext, false);
    }

    @OnClick({R.id.cancel, R.id.get_code, R.id.login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancel:
                finish();
                break;
            case R.id.get_code:
                if (!TextUtils.isEmpty(edAccount.getText().toString()) && edAccount.getText().toString().startsWith("1") && edAccount.getText().toString().length() == 11) {
                    loadingDialog.show();
                    getCode.startTimer();
                    AppApi.sendCode(edAccount.getText().toString(), 19, ForgetPasswordActivity.this, 1, ForgetPasswordActivity.this);
                } else {
                    Toast.makeText(mContext, "请填写您的手机号码！", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.login:
                List<EditText> editTexts = new ArrayList<>();
                edAccount.setTag("请输入您的手机号码！");
                newPassword.setTag("请输入您的新密码！");
                confirmPassword.setTag("请再次输入您的密码！");
                edCode.setTag("请输入您的验证码！");
                editTexts.add(edAccount);
                editTexts.add(newPassword);
                editTexts.add(confirmPassword);
                editTexts.add(edCode);
                if (MyUtils.isFormat(mRootView, editTexts)) {
                    if (newPassword.getText().toString().equals(confirmPassword.getText().toString())) {
                        loadingDialog.show();
                        AppApi.resetPassword(newPassword.getText().toString(), confirmPassword.getText().toString(), edAccount.getText().toString(), edCode.getText().toString(), ForgetPasswordActivity.this, 2, ForgetPasswordActivity.this);
                    } else {
                        SnackUtils.showError(mRootView, "两次输入的密码不一致");
                    }
                }
                break;
        }
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        switch (tag) {
            case 1:
                break;
            case 2:
                Toast.makeText(mContext, "密码重置成功！", Toast.LENGTH_SHORT).show();
                finish();
                break;
        }
    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }
}
